package startup

import (
	"shop-admin/task"
	"shop-common/cronTask"
	"shop-common/engine"
	"shop-sys/business"
)

func StartKnownScheduler(tasks []*engine.CronJob) (err error) {
	afterStartJobs()
	scheduler := cronTask.NewScheduler()
	for _, cronJob := range tasks {
		if err = scheduler.AddJobByFunc(cronJob); err != nil {
			return err
		}
	}
	scheduler.Start()
	return nil
}

const (
	//CronUploadTask = "0 */3 * * * ?"
	//CronUploadTask = "*/5 * * * * ?"
	//CronUploadTask = "0 0 14 * * ?" // 每天凌晨1点执行一次
	//CronUploadTask = "0 0 1 * * ?" // 每天每1小时触发一次
	CronUploadTask = "01 30 22 * * ?" // 每天22点30分01秒触发一次
)

func GenCronJobs() []*engine.CronJob {
	tasks := make([]*engine.CronJob, 0)
	tasks = append(tasks, &engine.CronJob{
		Name: "过期附件删除",
		Spec: CronUploadTask,
		Job:  task.DeleteBatchExpireFiles,
	})
	return tasks
}

//启动运行任务
func afterStartJobs() {
	_, _ = business.ServiceImpl().SysMenuService.FindByRoleIdPerms(nil)
}
